<?php
/*
Plugin Name: BPDEV-YOUTUBE
Plugin URI: http://bp-dev.org/projects/#aggregator
Description: Add a page to show own youtube videos for each user
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

define( 'BPDEV_YOUTUBE_NAME', 'BPDEV-YOUTUBE' );
define( 'BPDEV_YOUTUBE_VERSION', '0.3' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );
require_once( 'bpdev-youtube/bpdev-youtube-classes.php' );
if ( function_exists( 'simplepie_init' ) ) simplepie_init();

if ( !get_option( 'bpdev-youtube-status' ) )
	add_option( 'bpdev-youtube-status' , 'off');

if ( get_option( 'bpdev-youtube-status' ) == "on" ) {

	add_action( 'wp' , 'bpdev_youtube_setup_globals' , 1 ); //add globals on wp
	add_action( '_admin_menu' , 'bpdev_youtube_setup_globals' , 1 ); //add globals on _admin_menu
	add_action( 'wp' , 'bpdev_youtube_setup_nav' , 2 );
	add_action( 'bp_custom_profile_boxes' , 'youtube_show_profile' );
	add_action( 'bpdev_theme_extra_setup_globals' , 'bpdev_youtube_css' );

}

add_action( 'bpdev_admin_plugins_screen' , 'bpdev_youtube_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save' , 'bpdev_youtube_admin_menu_save', 3 );



function bpdev_youtube_setup_globals() {

	global $bp;

	$bp->bpdev->youtube->image_base = site_url() . '/wp-content/mu-plugins/bpdev-youtube/images';
	$bp->bpdev->youtube->slug = 'youtube';
	$bp->bpdev->youtube->css = site_url() . '/' . bpdev_component_dir( $bp->bpdev->youtube->slug ) . 'css/';
	$bp->bpdev->youtube->videobox = site_url() . '/' . bpdev_component_dir( $bp->bpdev->youtube->slug ) . 'video/';

}

function bpdev_youtube_admin_menu_save() {

	do_action( 'bpdev_admin_save_component' , 'bpdev-youtube-status' );

}

function bpdev_youtube_admin_menu_screen() {

	?>

	<h3><?php echo BPDEV_YOUTUBE_NAME; ?></h3>
	<table class="form-table">

		<tr valign="top">

			<th scope="row">Plugin Version</th>

			<td>
				<?php echo BPDEV_YOUTUBE_VERSION; ?>
			</td>

		</tr>

		<tr>

			<th scope="row">Plugin Status</th>

			<td>

				<select id="bpdev-youtube-status" name="bpdev-youtube-status">

					<option value="off"  <?php if (!get_option('bpdev-youtube-status') || get_option('bpdev-youtube-status') == "off") echo 'selected=""'; ?>>Off</option>

					<option value="on" <?php if(get_option('bpdev-youtube-status') == "on") echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>

	</table>

	<?php

}

function bpdev_youtube_setup_nav() {

	global $bp;

	bp_core_add_nav_item( __('Youtube', 'buddypress'), $bp->bpdev->youtube->slug );
	bp_core_add_nav_default( $bp->bpdev->youtube->slug, 'youtube_show_settings', 'show' ); //Set aggregator_blog_settings as default page
	$youtube_link = $bp->loggedin_user->domain. $bp->bpdev->youtube->slug . '/'; //link for pages
	
	/* Add the subnav items to the aggregator nav item */	
	bp_core_add_subnav_item( $bp->bpdev->youtube->slug, 'show', __('Show', 'buddypress'), $youtube_link, 'youtube_show_settings' );
/*	bp_core_add_subnav_item( $bp->bpdev->youtube->slug, 'settings', __('Settings', 'buddypress'), $youtube_link, 'youtube_option_settings', bp_is_home() );*/
	bp_core_add_subnav_item( 'settings', 'youtube', __('Youtube Settings', 'buddypress'), $bp->loggedin_user->domain. 'settings/', 'youtube_option_settings', false, bp_is_home() );

	if ( $bp->current_component == $bp->bpdev->youtube->slug ) {

		if ( bp_is_home() ) {
			$bp->bp_options_title = __('Youtube', 'buddypress'); //Area
		} else {
			$bp->bp_options_avatar = bp_core_get_avatar( $bp->displayed_user->id, 1 );
			$bp->bp_options_title = $bp->displayed_user->fullname; 
		}

	}

}
function youtube_show_settings() {

	add_action( 'bp_template_content' , 'youtube_show_content' );
//	add_action('youtube_show_content_right', 'blog_feed_content');
	add_action( 'bp_template_content_header' , 'youtube_show_title' );
	add_action( 'wp_head' , 'youtube_head_load_js');
	bp_catch_uri( 'plugin-template' );

}
function youtube_head_load_js() { 

	global $bp;
	?>
	
<!--BPDEV-YOUTUBE PLUGIN LOAD STYLE&CSS-->
	<script type="text/javascript" src="<?php echo $bp->bpdev->youtube->videobox; ?>js/mootools.js"></script>
	<script type="text/javascript" src="<?php echo $bp->bpdev->youtube->videobox; ?>js/swfobject.js"></script>
	<script type="text/javascript" src="<?php echo $bp->bpdev->youtube->videobox; ?>js/videobox.js"></script>
	<link rel="stylesheet" href="<?php echo $bp->bpdev->youtube->videobox; ?>css/videobox.css" type="text/css" media="screen" />
<!--BPDEV-YOUTUBE PLUGIN LOAD STYLE&CSS-->

	<?php

}
function bpdev_youtube_css() {

	bpdev_theme_register_style(
		'bpdev-youtube', 
		'BPDEV Youtube', 
		array( 'BPDEV_Theme_CSS' , 'bpdev_youtube' ),
		'on',
		'off'
	);

}

function youtube_show_title() {

	bp_word_or_name( __( "My Youtube Videos", 'buddypress' ), __( "%s's Youtube Videos", 'buddypress' ) );

}
function youtube_show_profile() {

	if (!function_exists('flickr_head_load_js') ) add_action('wp_head', 'youtube_head_load_js');

	$imageclass = new BPDEV_Youtube(); ?>
<div class="info-group">
	<h4>Youtube Videos <a href="<?php bpdev_youtube_see_all_link() ?>"><?php _e( "See All &raquo;", "buddypress" ) ?></a></h4>
	<ul class="item-list" id="yt-post-list">
		<?php $imageclass->profile_link(); ?>
	</ul>
</div>
	<?php

}
function youtube_show_content() {

	global $bp, $current_user, $bp_settings_updated, $pass_error; 
	$imageclass = new BPDEV_Youtube(); ?>
	<div class="left-menu">
		<?php bp_the_avatar() ?>
		
		<div class="button-block">
			<?php if ( function_exists('bp_add_friend_button') ) : ?>
				<?php bp_add_friend_button() ?>
			<?php endif; ?>
			
			<?php if ( function_exists('bp_send_message_button') ) : ?>
				<?php bp_send_message_button() ?>
			<?php endif; ?>
		</div>

		<?php bp_custom_profile_sidebar_boxes() ?>
	</div>

	<div class="main-column">
		<div class="inner-tube">
			<div id="profile-name">
				<h1 class="fn">
					<a href="<?php bp_user_link() ?>youtube"><?php bp_word_or_name( __( "My Youtube Videos", 'buddypress' ), __( "%s's Youtube Videos", 'buddypress' ) ); ?></a>
				</h1>
			</div>		
			<div class="info-group">
				<h4>Youtube Profile</h4>
<?php if ( function_exists('simplepie_init') and get_option('bpdev-simplepie-status') != "off" ) { ?>
				<table class="profile-fields">
					<tbody>
						<tr>
							<td class="label">Username</td>
							<td class="data">
								<p><a href="http://youtube.com/user/<?php echo $imageclass->id_number ?>"><?php echo $imageclass->id_number ?></a></p>
							</td>
						</tr>
					</tbody>
				</table>
			</div>
			<div class="info-group">
				<h4>Youtube Videos <a href="<?php bpdev_youtube_see_all_link() ?>"><?php _e( "See All &raquo;", "buddypress" ) ?></a></h4>
				<ul class="item-list" id="yt-post-list">
					<?php $imageclass->image_link(); ?>
				</ul>
			</div>
			<div class="info-group">
				<h4>Last Videos</h4>
				<ul class="horiz-gallery">
				     <?php $imageclass->image_horiz_link(); ?>
				</ul>
			</div>
<?php } else { ?>
<strong>This plugin will not work if you don't install <a href="http://buddypressdev.org/plugins/bpdev-simplepie">BPDEV-SIMPLEPIE LOADER plugin!</a></strong>
<?php } ?>
			<div class="button-block">
				<?php bpdev_youtube_credits(); ?>
			</div>
		</div>
	</div>
<?php 
	do_action( 'youtube_show_content_right' );

} 
function youtube_option_settings() {

	global $current_user, $bp_settings_updated, $pass_error;

	$bp_settings_updated = false;
	$pass_error = false;

	if (isset($_POST['save_bpdev_youtube_set_settings']) && check_admin_referer('bp_settings_youtube')) {

		$option_youtubeid = $_POST['youtube_id'];
		$option_tags = $_POST['tags'];
		$option_set = $_POST['set'];
		$option_show2type = $_POST['show2type'];
		$option_display_numitems = $_POST['display_numitems'];

		update_usermeta( (int)$current_user->id,'bpdev_youtube_set_youtubeid', $option_youtubeid);
		update_usermeta( (int)$current_user->id,'bpdev_youtube_set_tags', $option_tags);
		update_usermeta( (int)$current_user->id,'bpdev_youtube_set_set', $option_set);
		update_usermeta( (int)$current_user->id,'bpdev_youtube_set_show2type', $option_show2type);

	}

	add_action( 'bp_template_title', 'youtube_option_title' );
	add_action( 'bp_template_content', 'youtube_option_content' );
//	add_action( 'bp_template_content_header', 'youtube_option_tabs' );
	bp_catch_uri('plugin-template');
}
function youtube_option_title() {

	_e( 'Youtube Video Settings', 'buddypress' );

}
function youtube_option_content() {

	global $bp, $current_user, $bp_settings_updated, $pass_error;

	if ( $bp_settings_updated && !$pass_error ) { 
	?>
		<div id="message" class="updated fade">
			<p><?php _e( 'Changes Saved.', 'buddypress' ) ?></p>
		</div>
	<?php
	} 

	if ( $pass_error && !$bp_settings_updated ) {
	?>
		<div id="message" class="error fade">
			<p><?php _e( 'Error updating datas', 'buddypress' ) ?></p>
		</div>
	<?php } ?>

	<form action="<?php echo $bp->loggedin_user->domain. 'settings/youtube' ?>" method="post" id="settings-form">
		<table class="form-table">
			<tr valign="top">
				<th scope="row">ID Number</th>
				<td>http://youtube.com/user/
					<input name="youtube_id" type="text" id="youtube_id" value="<?php echo $current_user->bpdev_youtube_set_youtubeid; ?>" size="20" />
				</td>
			</tr>
			<tr valign="top">
				<th scope="row">Display</th>
				<td>
					<select name="show2type" id="show2type">
						<option <?php if( 'upload' == $current_user->bpdev_youtube_set_show2type ) { echo 'selected'; } ?> value="upload">Uploaded Videos</option>
						<option <?php if( 'favourite' == $current_user->bpdev_youtube_set_show2type ) { echo 'selected'; } ?> value="favourite">Favorite Videos</option>
						<!-- <option <?php if($current_user->bpdev_youtube_set_show2type == 'favorite') { echo 'selected'; } ?> value="favorite">favorite</option>
						<option <?php if($current_user->bpdev_youtube_set_show2type == 'group') { echo 'selected'; } ?> value="group">group</option>
						<option <?php if($current_user->bpdev_youtube_set_show2type == 'community') { echo 'selected'; } ?> value="community">community</option>-->
					</select>
				</td> 
			</tr>
<!--         <tr valign="top">
		  <th scope="row">Set</th>
          <td><input name="set" type="text" id="set" value="<?php echo $current_user->bpdev_youtube_set_set; ?>" size="40" /> Use number from the set url</p>
         </tr>
         <tr valign="top">
		  <th scope="row">Tags</th>
          <td><input name="tags" type="text" id="tags" value="<?php echo $current_user->bpdev_youtube_set_tags; ?>" size="40" /> Comma separated, no spaces</p>
         </tr>-->
		</table>

		<div class="submit">
			<input type="submit" name="save_bpdev_youtube_set_settings" value="<?php _e( 'Save Settings', 'save_bpdev_youtube_set_settings' ) ?>" />
			<?php wp_nonce_field( 'bp_settings_youtube' ) ?>
		</div>

        </form>

	<?php

}
function bpdev_youtube_see_all_link() {

	global $bp;
	
	if ( $bp->current_item == '')
		$uri = $bp->current_action;
	else
		$uri = $bp->current_item;

	echo apply_filters( 'bpdev_youtube_see_all_link', $bp->displayed_user->domain . $bp->bpdev->youtube->slug );

}
function bpdev_youtube_credits() {

	global $bp;
	if( bp_is_home() ) {
	?>
			<div class="generic-button">
				<a class="edit" title="Edit Profile" href="<?php bp_user_link() ?>settings/youtube">Edit Youtube Settings</a>
			</div>
			<div class="clear"></div>
	<?php } ?>
			<!-- <div class="generic-button"><a href="http://buddypressdev.org/plugins/bpdev-youtube" title="Show YouTube in BuddyPress">Powered by BPDEV-YOUTUBE</a></div>    -->
	<?php
	do_action( 'bpdev_youtube_credits' );

}
?>